package frobagroup.pagination;

import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import java.io.Serializable;

/**
 * Created by Tinsley on 2016/7/15.
 */
@NoArgsConstructor
@Data
public class PageableImpl implements Serializable,Pageable {
    private static final long serialVersionUID = 1232825578694716871L;
    private int pn ;
    private int pz ;
    private Sort sort;

    public PageableImpl(int pn, int pz){
        this.pn = pn;
        this.pz = pz;
    }

    public PageableImpl(int pn, int pz, Sort sort) {
        this(pn,pz);
        this.sort = sort;
    }

    @Override
    public int getPageNumber() {
        return getPn();
    }

    @Override
    public int getPageSize() {
        return getPz();
    }
    @Override
    public int getOffset() {
        return Math.max(0,(getPageNumber() - 1) * getPz());
    }
    @Override
    public Sort getSort() {
        return sort;
    }

    @Override
    public Pageable next() {
        return new PageableImpl(this.getPageNumber() + 1, this.getPageSize(), this.getSort());
    }

    @Override
    public Pageable previousOrFirst() {
        return this.getPageNumber() == 0?this:new PageableImpl(this.getPageNumber() - 1, this.getPageSize(), this.getSort());
    }

    @Override
    public Pageable first() {
        return new PageableImpl(1, this.getPageSize(), this.getSort());
    }

    @Override
    public boolean hasPrevious() {
        return this.getPn() > 0;
    }

}
